﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Ink;


namespace DentistApplication
{
    /// <summary>
    /// Interaction logic for PF_PersonalData.xaml
    /// </summary>
    public partial class PF_PersonalData : Window
    {
        int patientId;
        string teethClicked;

        public PF_PersonalData(int p)
        {
            InitializeComponent();
            this.patientId = p;
            fillPersonalData();
            txtboxNote.Text = DatabaseHandler.getPatient(p).notes;
        //    la_chosenPatient.Content += p.name + " " + p.lastname;

      //      this.teeth_controller.EditingMode = InkCanvasEditingMode.Ink;
          //  inkAttributes = new DrawingAttributes { Color=Color.FromRgb(10,10,10), Height=10, Width=10 };
         //   teeth_controller.DefaultDrawingAttributes = inkAttributes;
        }

    //    DrawingAttributes inkAttributes;

        private void button1_Click_2(object sender, RoutedEventArgs e)
        {
            
            Visit v = new Visit
            {
                date = DateTime.Now,
                id_patient = patientId,
                id_personel = 1
            };
            DatabaseHandler.addVisitToDatabase(v);
        }

        private void fillHeader(Patient p)
        {
            la_nameLastname.Content = p.firstname + " " + p.lastname;
            la_patientId.Content = "Numer kartoteki: " + p.id;            
        }

        private void fillPersonalData()
        {
            Patient p = DatabaseHandler.getPatient(patientId);
            box_patientName.Text = p.firstname;
            box_patientLastname.Text = p.lastname;
            box_patientPesel.Text = p.pesel;
            box_patientBirthDate.Text = p.birthdate.Value.Date.ToString();
            box_sex.Text = p.sex.Equals('M')?"Mężczyzna":(p.sex.Equals('F')?"Kobieta":(p.sex.Equals('O')?"Inny":"Nieznana"));
            box_phoneNr.Text = p.phoneNumber;
            box_mobileNr.Text = p.mobileNumber;
            box_emailAdd.Text = p.emailAddress;

            fillHeader(p);
            if ((p.notes == null) || (p.notes == ""))
                box_otherInfo.Text = "Brak dodatkowego opisu";
            else
                box_otherInfo.Text = p.notes;

        }

        private void fillBoxRecognitions()
        {
           List<Recognition> lr = DatabaseHandler.getPatientRecognitions(patientId);
           list_recognitions.Items.Clear();
            foreach (Recognition r in lr)
            {
                Recognition_type tr = DatabaseHandler.getRecognitionType(r.id_recognition_type);

                list_recognitions.Items.Add(r.id_visit + ") " + tr.name + "ząb: "+r.nr_tooth);
                addDottOnATeethDiagram(Int32.Parse(r.x.ToString()), Int32.Parse(r.y.ToString()), tr.color, Int32.Parse(tr.point_size.ToString()));
            }
        }

        private void fillBoxServices()
        {
            list_services.Items.Clear();
            List<Service> ls = DatabaseHandler.getPatientsAllServices(patientId);
            foreach (Service s in ls)
            {
                string serviceName = DatabaseHandler.getServiceTypeName(s.id_type);
                list_services.Items.Add(s.id + ") " + serviceName + " ząb: "+s.tooth);
            }
        }

        private void addDottOnATeethDiagram(int x, int y, string color, int size)
        { 
            /*
            teeth_controller.ad

                    MemoryStream memoryStream = new MemoryStream();

                this.board.Strokes.Save(memoryStream);

                memoryStream.Flush();*/

        }

        private void teeth_controller_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
        {
            try
            {
                MemoryStream memoryStream = new MemoryStream();

                this.teeth_controller.Strokes.Save(memoryStream);

                memoryStream.Flush();
                Canvas.SetZIndex(teeth_controller, 0);

                // DrawMeServiceClient.Instance.SendInkStrokes(memoryStream);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, Title);
            }
        }

        private void teeth_controller_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Point p = e.GetPosition(teeth_controller);
        }

        private void teeth_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Canvas.SetZIndex(teeth_controller, 10);
            Label l = (Label)sender;
            teethClicked = l.Name;
        }

        private void ribbonGroup_personalData_MouseDown(object sender, MouseButtonEventArgs e)
        {
            panel_personalData.Visibility = System.Windows.Visibility.Visible;
            panel_additional.Visibility = System.Windows.Visibility.Hidden;
            panel_interview.Visibility = System.Windows.Visibility.Hidden;
            panel_stanUzebienia.Visibility = System.Windows.Visibility.Hidden;
        }

        private void ribbonGroup_interview_MouseDown(object sender, MouseButtonEventArgs e)
        {
            panel_personalData.Visibility = System.Windows.Visibility.Hidden;
            panel_additional.Visibility = System.Windows.Visibility.Hidden;
            panel_interview.Visibility = System.Windows.Visibility.Visible;
            panel_stanUzebienia.Visibility = System.Windows.Visibility.Hidden;
        }

        private void ribbonGroup_teethSchema_MouseDown(object sender, MouseButtonEventArgs e)
        {
            panel_personalData.Visibility = System.Windows.Visibility.Hidden;
            panel_additional.Visibility = System.Windows.Visibility.Hidden;
            panel_interview.Visibility = System.Windows.Visibility.Hidden;
            panel_stanUzebienia.Visibility = System.Windows.Visibility.Visible;
        }

        private void ribbonGroup_additional_MouseDown(object sender, MouseButtonEventArgs e)
        {
            panel_personalData.Visibility = System.Windows.Visibility.Hidden;
            panel_additional.Visibility = System.Windows.Visibility.Visible;
            panel_interview.Visibility = System.Windows.Visibility.Hidden;
            panel_stanUzebienia.Visibility = System.Windows.Visibility.Hidden;
        }

        private void la_addPicture_MouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (DatabaseHandler.addNoteToPatient(this.patientId ,txtboxNote.Text))
            {
                MessageBox.Show("Dodano notatkę o pacjencie", "Aktualizacja");
            }
        }

    }
}